/*
     File: MapViewController.m 
 Abstract: The primary view controller containing the MKMapView, adding and removing both MKPinAnnotationViews through its toolbar. 
  Version: 1.2 
 Copyright (C) 2010 Apple Inc. All Rights Reserved. 
  
*/

#import "MapViewController.h"
#import "DetailViewController.h"
#import "SFAnnotation.h"
#import "DamAnnotation.h"
#import "DamInfo.h"

enum
{
    kDamAnnotationIndex  = 0
};

@implementation MapViewController

@synthesize mapView, detailViewController, mapAnnotations;


#pragma mark -

+ (CGFloat)annotationPadding;
{
    return 10.0f;
}
+ (CGFloat)calloutHeight;
{
    return 40.0f;
}

- (void)gotoLocation
{
    // start off by default in San Francisco
    MKCoordinateRegion newRegion;
	
	//newRegion.center.latitude     = 35.700622625387766;
	newRegion.center.latitude     = 40;
    //newRegion.center.longitude    = 139.7782051563263;
	newRegion.center.longitude    = 138;
    newRegion.span.latitudeDelta  = 18.112872;
    newRegion.span.longitudeDelta = 0.109863;

    [self.mapView setRegion:newRegion animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    // bring back the toolbar
    //[self.navigationController setToolbarHidden:NO animated:NO];
}

- (void)viewDidLoad
{
	// デリゲートを呼び出し
	appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
	
    self.mapView.mapType = MKMapTypeStandard;   // also MKMapTypeSatellite or MKMapTypeHybrid
	//self.mapView.mapType = MKMapTypeHybrid;   // also MKMapTypeSatellite or MKMapTypeHybrid

    // create a custom navigation bar button and set it to always says "Back"
	/*
	UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
	temporaryBarButtonItem.title = @"Back";
	self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
	[temporaryBarButtonItem release];
    */
	
    // create out annotations array (in this example only 2)
    self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:[appDelegate.damInfoArray count]];
    
	// ダム情報オブジェクト
	DamInfo       *damInfoObj;
	// ダムアノテーション
	DamAnnotation *damAnnotation;
	
	int i;
	for (i = 0; i < [appDelegate.damInfoArray count]; i++) {
		
		// ダム配列からオブジェクト取得
		damInfoObj = [appDelegate.damInfoArray objectAtIndex:i];
		
		// ダムアノテーション
		damAnnotation = [[DamAnnotation alloc] init];
				
NSLog(@"%d name:%@ x:%@ y:%@",i, damInfoObj.dam_name,damInfoObj.point_x, damInfoObj.point_y);
		damAnnotation.longitude = [damInfoObj.point_x doubleValue];
		damAnnotation.latitude  = [damInfoObj.point_y doubleValue];
		damAnnotation.title     = damInfoObj.dam_name;
		damAnnotation.subTitle  = [NSString stringWithFormat:@"%@%@",damInfoObj.ken_name, damInfoObj.address];
		
        [self.mapView addAnnotation:damAnnotation];
	}	
	[damAnnotation release];
	
	/*
    // ダムアノテーション
    DamAnnotation *damAnnotation = [[DamAnnotation alloc] init];
    
	[self.mapAnnotations insertObject:damAnnotation atIndex:kDamAnnotationIndex];
	[damAnnotation release];
    */
		
    [self gotoLocation];    // 日本地図表示
	//[self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:kDamAnnotationIndex]];
}

- (void)viewDidUnload
{
    self.mapAnnotations = nil;
    self.detailViewController = nil;
    self.mapView = nil;
}

- (void)dealloc 
{
    [mapView release];
    [detailViewController release];
    [mapAnnotations release];
    
    [super dealloc];
}


#pragma mark -
#pragma mark ButtonActions


- (IBAction)damAction:(id)sender
{
    [self gotoLocation];
    [self.mapView removeAnnotations:self.mapView.annotations];  // remove any annotations that exist
    [self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:kDamAnnotationIndex]];
}


#pragma mark -
#pragma mark MKMapViewDelegate

- (void)showDetails:(id)sender
{
    // the detail view does not want a toolbar so hide it
    //[self.navigationController setToolbarHidden:YES animated:NO];
    //[self.navigationController pushViewController:self.detailViewController animated:YES];
	[self pushViewController:self.detailViewController animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    
    // handle our two custom annotations 
    //
    if ([annotation isKindOfClass:[DamAnnotation class]]) // for ダム表示
    {
        // try to dequeue an existing pin view first
        static NSString* DamAnnotationIdentifier = @"damAnnotationIdentifier";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)
                                        [mapView dequeueReusableAnnotationViewWithIdentifier:DamAnnotationIdentifier];
        if (!pinView)
        {
            // if an existing pin view was not available, create one
            MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                             initWithAnnotation:annotation reuseIdentifier:DamAnnotationIdentifier] autorelease];
            customPinView.pinColor       = MKPinAnnotationColorGreen;
            customPinView.animatesDrop   = YES;
            customPinView.canShowCallout = YES;
            
            // add a detail disclosure button to the callout which will open a new view controller page
            //
            // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
            //  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
            //
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    
    
    return nil;
}

@end
